iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 25
0
自我挑戰組

ASP.NET MVC5從入門到退坑系列 第 25

[Day25]電子商城(五)

  • 分享至 

  • xImage
  •  

建立商品的ViewModel

 public class CartView
    {
        public List<CartBuy> DataList { get; set; }
        public bool isCartsave { get; set; }
    }

建立購物車功能模組

public class CartService
    {
        private ShoppingEntities db = new ShoppingEntities();

      
        public List<CartBuy> GetItemFromCart(string Cart)
        {

            return db.CartBuy.Where(p => p.Cart_Id == Cart).ToList();
        }

        
        public bool CheckInCart(string Cart, int Item_Id)
        {
 
            CartBuy SearchData = db.CartBuy
                .Where(p => p.Cart_Id == Cart && p.Item_Id == Item_Id)
                .FirstOrDefault();
            return (SearchData != null);
        }
  
        public bool CheckCartSave(string user, string Cart)
        {
          
            CartSave SearchData = db.CartSave
                .Where(p => p.Account == user && p.Cart_Id == Cart)
                .FirstOrDefault();
            return (SearchData != null);
        }
      
        public string GetCartSave(string user)
        {
           
            CartSave SearchData = db.CartSave
                .Where(p => p.Account == user).FirstOrDefault();
            if (SearchData != null)
            {
                return SearchData.Cart_Id;
            }
            else
            {
                return null;
            }
        }
       
        public void AddtoCart(string Cart, int Item_Id)
        {
         
            CartBuy newData = new CartBuy();
            newData.Cart_Id = Cart;
            newData.Item_Id = Item_Id;
            db.CartBuy.Add(newData);
            db.SaveChanges();
        }
  

        
        public void RemoveForCart(string Cart, int Item_Id)
        {
          
            CartBuy DeleteData = db.CartBuy
                .Where(p => p.Cart_Id == Cart && p.Item_Id == Item_Id)
                .FirstOrDefault();
            db.CartBuy.Remove(DeleteData);
            db.SaveChanges();
        }



        public void SaveCart(string user, string Cart)
        {
            CartSave newData = new CartSave();
            newData.Account = user;
            newData.Cart_Id = Cart;
            db.CartSave.Add(newData);
              db.SaveChanges();
        }


        public void SaveCartRemove(string user)
        {
            CartSave DeleteData = db.CartSave
                .Where(p => p.Account == user).FirstOrDefault();
            db.CartSave.Remove(DeleteData);
            db.SaveChanges();
        }
    }

上一篇
[Day24]電子商城(四)
下一篇
[Day26]電子商城(六)
系列文
ASP.NET MVC5從入門到退坑30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言